package com.cskaoyan.service;

import com.cskaoyan.bean.MarketFootprint;
import com.cskaoyan.bean.MarketFootprintExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketGoodsExample;
import com.cskaoyan.bean.collection.AppletFootprintListData;
import com.cskaoyan.bean.collection.AppletFootprintListVO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期: 2022/05/07 15:04
 *
 * @author liutao
 */
@Service
public class FootprintServiceImpl implements FootprintService{

    @Autowired
    MarketFootprintMapper marketFootprintMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public AppletFootprintListVO footprintList(BaseParam baseParam, Integer id) {
        List<AppletFootprintListData> appletFootprintListData = new ArrayList<>();

        Integer page = baseParam.getPage();
        Integer limit = baseParam.getLimit();
        PageHelper.startPage(page, limit);

        MarketFootprintExample example = new MarketFootprintExample();
        example.setOrderByClause("add_time" + " desc");
        MarketFootprintExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        // 用户id找到物品id,物品id找到物品信息
        List<MarketFootprint> marketFootprintList = marketFootprintMapper.selectByExample(example);
        for (MarketFootprint marketFootprint : marketFootprintList) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(marketFootprint.getGoodsId());
            AppletFootprintListData data = new AppletFootprintListData(marketGoods.getBrief(), marketGoods.getPicUrl(), marketGoods.getAddTime(),
                    marketGoods.getId(), marketGoods.getName(), marketFootprint.getId(), marketGoods.getRetailPrice());
            appletFootprintListData.add(data);
        }
        PageInfo<AppletFootprintListData> pageInfo = new PageInfo<>(appletFootprintListData);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return AppletFootprintListVO.data(total, pages, limit, page, appletFootprintListData);
    }

    @Override
    public void footprintDelete(MarketFootprint marketFootprint) {
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        criteria.andIdEqualTo(marketFootprint.getId());
        int i = marketFootprintMapper.updateByExampleSelective(marketFootprint, marketFootprintExample);
    }
}
